﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
/// <summary>
/// DateBase 的摘要说明
/// </summary>
public class DataBase
{
    private static DataBase _instance;

    public DataBase()
    {
        //
        // TODO: 在此处添加构造函数逻辑
        //
    }

    public static DataBase getInstance()
    {
        if (_instance == null)
        {
            _instance = new DataBase();
        }
        return _instance;
    }
    public static SqlConnection getConnection()
    {
        string strConn = System.Configuration.ConfigurationManager.AppSettings["app_conn_str"].ToString();
        SqlConnection conn = new SqlConnection(strConn);
        if (conn.State.Equals(ConnectionState.Closed))
        {
            conn.Open();
        }
        return conn;
    }

    public static SqlCommand createCmd(string paraName, SqlParameter[] parameters)
    {
        SqlConnection conn = getConnection();
        SqlCommand cmd = new SqlCommand();
        //cmd.CommandType = CommandType.StoredProcedure;
        cmd.Connection = conn;
        cmd.CommandText = paraName;
        if (parameters != null)
        {
            foreach (SqlParameter p in parameters)
            {
                if (p != null)
                {
                    cmd.Parameters.Add(p);
                }
            }
        }
        return cmd;

    }

    public static SqlCommand createCmd(string paraName, SqlParameter[] parameters, SqlConnection con)
    {
        SqlConnection conn = con;
        if (conn.State.Equals(ConnectionState.Closed))
        {
            conn.Open();
        }
        SqlCommand cmd = new SqlCommand();
        //cmd.CommandType = CommandType.StoredProcedure;
        cmd.Connection = conn;
        cmd.CommandText = paraName;
        if (parameters != null)
        {
            foreach (SqlParameter p in parameters)
            {
                if (p != null)
                {
                    cmd.Parameters.Add(p);
                }
            }
        }
        return cmd;

    }

    public static SqlCommand createCmd(string paraName, SqlConnection con)
    {
        SqlConnection conn = con;
        if (conn.State.Equals(ConnectionState.Closed))
        {
            conn.Open();
        }
        SqlCommand cmd = new SqlCommand();
        cmd.CommandType = CommandType.StoredProcedure;
       // cmd.Connection = conn;
        cmd.CommandText = paraName;

        return cmd;

    }

    public static SqlCommand createCmd(string paraName)
    {
        SqlConnection conn = getConnection();

        SqlCommand cmd = new SqlCommand();
       // cmd.CommandType = CommandType.StoredProcedure;
        cmd.Connection = conn;
        cmd.CommandText = paraName;

        return cmd;

    }

    public static SqlDataReader runProcGetReader(string procName, SqlParameter[] parameters)
    {
        SqlCommand cmd = createCmd(procName, parameters);
        SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
        return dr;
    }

    public static SqlDataReader runPorcGetReader(string procName, SqlParameter[] parameters, SqlConnection conn)
    {
        SqlCommand cmd = createCmd(procName, parameters, conn);
        SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
        return dr;
    }

    public static SqlDataReader runPorcGetReader(string procName, SqlConnection conn)
    {
        SqlCommand cmd = createCmd(procName, conn);
        SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
        return dr;
    }

    public static SqlDataReader runPorcGetReader(string procName)
    {
        SqlCommand cmd = createCmd(procName);
        SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
        return dr;
    }

    public static DataTable runProcGetTable(string procName, SqlParameter[] parameters, SqlConnection conn)
    {
        SqlCommand cmd = createCmd(procName, parameters, conn);
        SqlDataAdapter da = new SqlDataAdapter();
        DataSet ds = new DataSet();
        da.SelectCommand = cmd;
        da.Fill(ds);
        DataTable dt = ds.Tables[0];
        conn.Close();
        return dt;

    }

    public static DataSet runProcGetSet(string procName, SqlParameter[] parameters, SqlConnection conn)
    {
        SqlCommand cmd = createCmd(procName, parameters, conn);
        SqlDataAdapter da = new SqlDataAdapter();
        DataSet ds = new DataSet();
        da.SelectCommand = cmd;
        da.Fill(ds);
        conn.Close();
        return ds;

    }


    public static DataTable runProcGetTable(string procName, SqlParameter[] parameters)
    {
        SqlConnection conn = getConnection();
        SqlCommand cmd = createCmd(procName, parameters, conn);
        SqlDataAdapter da = new SqlDataAdapter();
        DataSet ds = new DataSet();
        da.SelectCommand = cmd;
        da.Fill(ds);
        DataTable dt = ds.Tables[0];
        conn.Close();
        return dt;

    }


    public static DataSet runProcGetSet(string procName, SqlParameter[] parameters)
    {
        SqlConnection conn = getConnection();
        SqlCommand cmd = createCmd(procName, parameters, conn);
        SqlDataAdapter da = new SqlDataAdapter();
        DataSet ds = new DataSet();
        da.SelectCommand = cmd;
        da.Fill(ds);
        conn.Close();
        return ds;

    }


    public static DataSet runProcGetSet(string procName)
    {
        SqlConnection conn = getConnection();
        SqlCommand cmd = createCmd(procName);
        SqlDataAdapter da = new SqlDataAdapter();
        DataSet ds = new DataSet();
        da.SelectCommand = cmd;
        da.Fill(ds);
        conn.Close();
        return ds;

    }

    public static int runExecute(string procName)
    {

        SqlCommand cmd = createCmd(procName);
        return cmd.ExecuteNonQuery();
        //return 0;
    }

    public static int runExecute(string procName, SqlParameter[] patameters)
    {
        SqlCommand cmd = createCmd(procName, patameters);
        return cmd.ExecuteNonQuery();
        //return 0;
    }

    public static int runExecuteScalar(string procName)
    {
        SqlCommand cmd = createCmd(procName);
        return Convert.ToInt32(cmd.ExecuteScalar());
        //return 0;
    }

    public static int runExecuteScalar(string procName, SqlParameter[] parameters)
    {
        SqlCommand cmd = createCmd(procName, parameters);
        return Convert.ToInt32(cmd.ExecuteScalar());
        //return 0;
    }

}

